package com.car.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.account.controller.form.adminForm.*;
import com.car.account.controller.form.userForm.SelectUserInfoDtoForm;
import com.car.account.dto.adminDto.AdminDto;
import com.car.account.dto.adminDto.SelectAdminDto;
import com.car.account.dto.adminDto.SelectById;
import com.car.account.dto.userDto.UserInfoByIdDto;
import com.car.account.dto.userDto.UserInfoDto;
import com.car.account.entity.Admin;
import com.car.account.entity.PlatformRole;
import com.car.account.exception.AccountException;
import com.car.account.param.adminParam.InsertAdminParam;
import com.car.account.param.adminParam.SelectAdminDtoParam;
import com.car.account.param.adminParam.UpdateRoleParam;
import com.car.account.param.userParam.SelectUserInfoDtoParam;
import com.car.account.service.AdminService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "平台账号")
public class AdminController {

    @Resource
    private AdminService adminService;

    @PostMapping("/adminPassLogin")
    @ApiOperation(value = "管理员密码登录")
    public Result adminPassLogin(@RequestBody AdminPassLoginForm form) throws Exception{
        Integer adminId = adminService.adminPassLogin(form);
        Map<String, Object> map = new HashMap<>();
        map.put("adminId",adminId);
        String token = JwtUtil.createAdminToken(map, 24 * 60);
        WebUtil.getResponse().setHeader("car-token",token);
        WebUtil.getResponse().setHeader("Access-Control-Expose-Headers","car-token");
        return Result.getResult().setMsg("管理员登录成功");
    }

    @GetMapping("/sendAdminLoginCode")
    @ApiOperation(value = "发送管理员登录验证码")
    public Result sendAdminLoginCode(String adminPhone) throws Exception{
        adminService.sendAdminLoginCode(adminPhone);
        return Result.getResult().setMsg("登录短信验证码发送成功");
    }

    @PostMapping("/adminCodeLogin")
    @ApiOperation(value = "管理员验证码登录")
    public Result adminCodeLogin(@RequestBody AdminCodeLoginForm form) throws Exception{
        Integer adminId = adminService.adminCodeLogin(form);
        Map<String, Object> map = new HashMap<>();
        map.put("adminId",adminId);
        String token = JwtUtil.createAdminToken(map, 24 * 60);
        WebUtil.getResponse().setHeader("car-token",token);
        WebUtil.getResponse().setHeader("Access-Control-Expose-Headers","car-token");
        return Result.getResult().setMsg("管理员登录成功");
    }

    @GetMapping("/selectAdminDto.pt")
    @ApiOperation(value = "管理员查询自己信息")
    public Result<AdminDto> selectAdminDto() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        AdminDto adminDto = adminService.selectAdminDto(adminId);
        return Result.getResult(AdminDto.class).setData(adminDto).setMsg("查询当前管理员信息成功");
    }

    @PostMapping("/selectAdmins.pt")
    @ApiOperation(value = "超管根据姓名，手机号，状态模糊查询管理员信息")
    public Result<Page> selectAdmins(@RequestBody SelectAdminDtoForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        SelectAdminDtoParam param = BeanUtil.toBean(form, SelectAdminDtoParam.class);
        param.setCurrentAdminId(currentAdminId);
        Page<SelectAdminDto> page = adminService.selectAdmins(param);
        return Result.getResult(Page.class).setData(page).setMsg("超管查询管理员信息成功");
    }

    @GetMapping("/disableUserByUserId.pt")
    @ApiOperation(value = "管理员根据用户id禁用用户")
    public Result disableUserByUserId(Integer userId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.disableUserByUserId(adminId, userId);
        return Result.getResult().setMsg("禁用用户成功");
    }

    @GetMapping("/enableUserByUserId.pt")
    @ApiOperation(value = "管理员根据用户id解禁用户")
    public Result enableUserByUserId(Integer userId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.enableUserByUserId(adminId, userId);
        return Result.getResult().setMsg("解禁用户成功");
    }

    @GetMapping("/deleteUserByUserId.pt")
    @ApiOperation(value = "管理员根据用户id删除用户")
    public Result deleteUserByUserId(Integer userId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.deleteUserByUserId(adminId, userId);
        return Result.getResult().setMsg("删除用户成功");
    }

    @GetMapping("/silenceUserByUserId.pt")
    @ApiOperation(value = "管理员根据用户id禁言用户")
    public Result silenceUserByUserId(Integer userId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.silenceUserByUserId(adminId, userId);
        return Result.getResult().setMsg("禁言用户成功");
    }

    @GetMapping("/unbanUserByUserId.pt")
    @ApiOperation(value = "管理员根据用户id解除禁言用户")
    public Result unbanUserByUserId(Integer userId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.unbanUserByUserId(adminId, userId);
        return Result.getResult().setMsg("解除禁言用户成功");
    }

    @GetMapping("/adminSelectUserInfoDtoById.pt")
    @ApiOperation(value = "管理员根据id查询用户信息")
    public Result<UserInfoByIdDto> adminSelectUserInfoDtoById(Integer userId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        UserInfoByIdDto userInfoByIdDto = adminService.adminSelectUserInfoDtoById(adminId, userId);
        return Result.getResult(UserInfoByIdDto.class).setData(userInfoByIdDto)
                .setMsg("管理员根据用户id查询用户信息成功");
    }

    @PostMapping("/adminSelectUserInfoDto.pt")
    @ApiOperation(value = "管理员根据姓名，手机号，状态模糊查询用户信息")
    public Result<Page> adminSelectUserInfoDto(@RequestBody SelectUserInfoDtoForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        SelectUserInfoDtoParam param = BeanUtil.toBean(form, SelectUserInfoDtoParam.class);
        param.setAdminId(adminId);
        Page<UserInfoDto> page = adminService.adminSelectUserInfoDto(param);
        return Result.getResult(Page.class).setData(page)
                .setMsg("管理员根据条件模糊查询用户信息成功");
    }

    @GetMapping("/updateAdminPhoneById.pt")
    @ApiOperation(value = "管理员修改绑定手机号")
    public Result updateAdminPhoneById(String adminPhone) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.updateAdminPhoneById(adminId, adminPhone);
        return Result.getResult().setMsg("修改管理员绑定手机成功");
    }

    @GetMapping("/updateAdminPassById.pt")
    @ApiOperation(value = "管理员修改密码")
    public Result updateAdminPassById(String adminPass) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        adminService.updateAdminPassById(adminId, adminPass);
        return Result.getResult().setMsg("修改管理员密码成功");
    }

    @PostMapping("/adminUpdateImage.pt")
    @ApiOperation(value = "管理员更改头像")
    public Result<String> adminUpdateImage(MultipartFile mf) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        String imageName = adminService.adminUpdateImage(mf, adminId);
        return Result.getResult(String.class).setData(imageName).setMsg("更换头像成功");
    }

    @GetMapping("/selectAdminRole.pt")
    @ApiOperation(value = "超管查询所有管理员身份")
    public Result<List<PlatformRole>> selectAdminRole() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        List<PlatformRole> roles = adminService.selectAdminRole(adminId);
        return Result.getListResult(PlatformRole.class).setData(roles).setMsg("超管查询所有管理员身份成功");
    }

    @GetMapping("/selectAdminAllRole.pt")
    @ApiOperation(value = "超管查询当前管理员身份id")
    public Result<Integer> selectAdminAllRole(Integer adminId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        Integer roleId = adminService.selectAdminAllRole(currentAdminId, adminId);
        return Result.getResult(Integer.class).setData(roleId).setMsg("超管查询当前管理员身份id成功");
    }

    @PostMapping("/updateAdminRole.pt")
    @ApiOperation(value = "超管根据身份id修改管理员角色")
    public Result updateAdminRole(@RequestBody UpdateRoleForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        UpdateRoleParam param = BeanUtil.toBean(form, UpdateRoleParam.class);
        param.setCurrentAdminId(currentAdminId);
        adminService.updateAdminRole(param);
        return Result.getResult().setMsg("超管根据身份id修改管理员角色成功");
    }

    @PostMapping("/insertAdmin.pt")
    @ApiOperation(value = "超管新增管理员")
    public Result insertAdmin(@RequestBody InsertAdminForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        InsertAdminParam param = BeanUtil.toBean(form, InsertAdminParam.class);
        param.setAdminId(adminId);
        adminService.insertAdmin(param);
        return Result.getResult().setMsg("超管新增管理员成功");
    }

    @GetMapping("/disableAdmin.pt")
    @ApiOperation(value = "超管禁用管理员")
    public Result disableAdmin(Integer adminId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        adminService.disableAdmin(currentAdminId, adminId);
        return Result.getResult().setMsg("禁用管理员成功");
    }

    @GetMapping("/enableAdmin.pt")
    @ApiOperation(value = "超管解禁管理员")
    public Result enableAdmin(Integer adminId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        adminService.enableAdmin(currentAdminId, adminId);
        return Result.getResult().setMsg("解禁管理员成功");
    }

    @GetMapping("/deleteAdmin.pt")
    @ApiOperation(value = "超管删除管理员")
    public Result deleteAdmin(Integer adminId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        adminService.deleteAdmin(currentAdminId, adminId);
        return Result.getResult().setMsg("删除管理员成功");
    }

    /**
     * 通过平台管理员id的set集合，返回平台管理员dto的list集合
     * @param adminIds
     * @return
     */
    @GetMapping("/adminsByIds.ty")
    @ApiOperation(value = "根据ids返回dto")
    public Result<List<SelectAdminDto>> findsAdminByIds(@RequestParam("adminIds") Set<Integer> adminIds) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentAdminId = JwtUtil.getAdminId(token);
        List<SelectAdminDto> adminDtos = adminService.findsAdminByIds(currentAdminId, adminIds);
        return Result.getListResult(SelectAdminDto.class).setData(adminDtos).setMsg("根据ids返回dto成功");
    }

    /**
     * 通过平台管理员id，返回平台管理员dto
     * @param adminId
     * @return
     */
    @GetMapping("/selectById.ty")
    @ApiOperation(value = "根据id返回dto")
    public Result<SelectById> selectById(@RequestParam("adminId") Integer adminId) throws Exception{
        Admin admin = adminService.getById(adminId);
        if (admin == null) throw new AccountException(2001,"不存在管理员信息");
        return Result.getResult(SelectById.class)
                .setData(BeanUtil.toBean(admin, SelectById.class))
                .setMsg("根据id返回dto成功");
    }

}

